package com.yunsc.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.exception.YunscException;
import com.yunsc.content.dao.YunscArticleCategoryDao;
import com.yunsc.content.dto.ArticleCategoryDTO;
import com.yunsc.content.entity.YunscArticleCategoryEntity;
import com.yunsc.content.service.YunscArticleCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("yunscArticleCategoryService")
public class YunscArticleCategoryServiceImpl extends ServiceImpl<YunscArticleCategoryDao, YunscArticleCategoryEntity> implements YunscArticleCategoryService {


    @Override
    public List<YunscArticleCategoryEntity> categoryList(Integer storeId) {
        return this.list(new QueryWrapper<YunscArticleCategoryEntity>().eq("store_id", storeId));
    }

    @Override
    public void addCategory(ArticleCategoryDTO param, Integer storeId) {
        YunscArticleCategoryEntity entity = new YunscArticleCategoryEntity();
        BeanUtils.copyProperties(param.getForm(), entity);
        entity.setStoreId(storeId);
        this.save(entity);
    }

    @Override
    public void updateCategory(ArticleCategoryDTO param) {
        Integer categoryId = param.getCategoryId();

        YunscArticleCategoryEntity entity = this.getById(categoryId);
        if (entity == null) {
            throw new YunscException("分类不存在");
        }

        BeanUtils.copyProperties(param.getForm(), entity);
        entity.setCategoryId(categoryId);
        this.updateById(entity);
    }

    @Override
    public void deleteCategory(Integer categoryId) {
        this.removeById(categoryId);
    }
}